package com.dapeis.core.spring.event.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import com.ag.module.common.api.service.SMSService;
import com.ag.module.common.event.impl.SMSAbstractSendChannel1Event;
import com.ag.spring.event.CommonEvent;
import com.ag.utils.DictionarySettingUtils;
import com.ag.utils.SpringUtils;
import com.ag.utils.SysConstant;

@Component
public class SmSendChannelEvent extends SMSAbstractSendChannel1Event implements
	ApplicationListener<CommonEvent> {

    public void initForSet() {
	String sendCount = DictionarySettingUtils
		.getParameterValue("admin.sms.channel1.sendCount");
	String sendPwd = DictionarySettingUtils
		.getParameterValue("admin.sms.channel1.sendPwd");
	String sendserverIp = DictionarySettingUtils
		.getParameterValue("admin.sms.channel1.sendServerIp");

	// logger.info("DictionarySettingUtils::::::::::" +
	// AGsonBuilder.create().toJson(DictionarySettingUtils.allSettings));

	if (StringUtils.isNotBlank(sendCount)
		&& StringUtils.isNotBlank(sendPwd)
		&& StringUtils.isNotBlank(sendserverIp)) {
	    accout = sendCount;
	    pswd = sendPwd;
	    serverIP = sendserverIp;
	    logger.info("设置短信渠道:[" + getChannelSend() + "],账号:[" + accout
		    + "],密码:[" + sendPwd + "],地址：[" + sendserverIp + "]");
	} else {
	    accout = "N3598377";
	    pswd = "Ps7932f9";
	    serverIP = "sms.dapeis.com";
	    logger.warn("短信发送渠道[{}]并没有设置账号密码，将采用默认!", getChannelSend());
	}
    }

    @Override
    public void onApplicationEvent(CommonEvent event) {
	if (SysConstant.SysCmd.INIT.equals(event.getCmd())) {
	    SMSService smsService = SpringUtils.getBean(SMSService.class);
	    smsService.setDefaultSMSSendChannel(this);
	}
    }
}
